const {
  NAME_OR_PASSWORD_IS_NOT_REQUIRED,
  USER_ALREADY_EXISIS,
  USER_DOES_NOT_EXISIS,
  PASSWORD_ERROR,
  UNAUTHORRIZATION,
  UNPERMISSION
} = require("../contants/error-types")

const errorHandler = (err,ctx) => {
  console.log("aaa",err.message);
  let status,message;
  switch (err.message) {
    case NAME_OR_PASSWORD_IS_NOT_REQUIRED:
      message = '用户名或密码不能为空';
      status = 400 //Bad Request
      break;

    case USER_ALREADY_EXISIS:
      message = '用户已存在';
      status = 409 //conflict
      break;
    
    case USER_DOES_NOT_EXISIS:
      message = '用户不存在';
      status = 400
      break;
    
    case PASSWORD_ERROR:
      message = '密码错误';
      status = 400
      break;
    case UNAUTHORRIZATION:
      message = '无效的token'
      status = 401
      break;
      
    case UNPERMISSION:
      message = '你不具备操作的权限'
      status = 401
      break;

    default:
      status = 404;
      message = 'NOT FOUND';
      break;
  }
  ctx.status = status
  ctx.body = message
}

module.exports = errorHandler